﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CommonFunc;
using SQLExpressDAL;
using SQLExpressDAL.UsersTableAdapters;
using System.Text;
using System.Data.SqlClient;

public partial class Control_UserReg : System.Web.UI.UserControl
{
    UsersTableAdapter adp = new UsersTableAdapter();
    private string loginPage;
    int num;
    String msg = String.Empty;
    String sql;

    public string LoginPage
    {
        get { return loginPage; }
        set { loginPage = value; }
    }

    protected string DeductMoney;
    protected string MemAccountMoney;
    SQLExpressDAL.DealRule.WebSiteConfigDataTable configTable = null;

    MODEL.Users MU = new MODEL.Users();
    MODEL.Users _MU = new MODEL.Users();


    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!Page.IsPostBack) 
        //{
            InitData();
        //}
            SQLExpressDAL.DealRuleTableAdapters.WebSiteConfigTableAdapter configAdp = new SQLExpressDAL.DealRuleTableAdapters.WebSiteConfigTableAdapter();
            configTable = configAdp.GetData();
            DataRow[] drs = configTable.Select(" configname='DeductMoney'");
            DeductMoney = drs[0]["configvalue"].ToString();
              drs = configTable.Select(" configname='MemAccountMoney'");
            MemAccountMoney = drs[0]["configvalue"].ToString();

    }

    private void InitData()
    {
        //获取最新系统ID
        TB_LoginID.Text = SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "UP_GetUserNO", null).ToString();
        TB_ParentID.Text = Session["userid"].ToString();
    }

    #region 注册按钮
    protected void Btn_Submit_Click(object sender, EventArgs e)
    {
        //校验数据
        if (!this.CheckData())
        {
            return;
        }
        try
        {
            MU.UserID = TB_LoginID.Text;
            MU.UserName = TB_LoginName.Text;   // 3,
            MU.UserPassword = TB_Pass.Text;   // 4,
            MU.UserSex = Convert.ToByte(DL_Sex.SelectedValue);   // 5,
            MU.UserPhone = TB_Tel.Text;   // 6,
            MU.UserMobile = TB_Mobile.Text;   // 7,
            MU.UserEmail = TB_Email.Text;   // 8,
            MU.UserCountry = TB_Country.Text;   // 9,
            MU.UserCity = TB_City.Text;   // 10,
            MU.UserCardCode = TB_CardCode.Text;   // 11,
            MU.UserAddress = TB_Address.Text;   // 12,
            MU.UserPostCode = TB_PostCode.Text;   // 13,
            MU.AddDate = DateTime.Now; ;   // 14,
            MU.Cdefine1 = TB_PaiXian.Text;   // 15,
            MU.Cdefine2 = "0";   // 16,
            MU.parentID = TB_ParentID.Text;
            MU.Cdefine5 = TB_MiYao.Text;     //密钥
            MU.Cdefine6 = TB_RegName.Text;  //真实姓名

            int row = adp.Insert(MU.UserID, MU.UserName, MU.UserPassword, MU.UserPhone, MU.UserMobile, MU.UserEmail, MU.UserCountry, MU.UserCity,
                MU.UserCardCode, MU.UserAddress, MU.UserPostCode, MU.AddDate, MU.Cdefine1, MU.Cdefine2,0,MU.parentID,MU.UserSex,MU.Cdefine5,MU.Cdefine6);

            if (row > 0)
            {
                string js;
                js = ("<script language='javascript'> alert('会员注册成功，马上进入登录页面！');document.location.href='MemberLogin.aspx';</script>");
                Page.RegisterClientScriptBlock("",js);
                //MessageBox(this, "会员注册成功，马上进入登录页面！", "MemberLogin.aspx");
            }
            else {
                Page.RegisterClientScriptBlock("", "<script language='javascript'> alert('会员注册失败！');</script>");
                return;
            }
        }
        catch (Exception ex)
        {
            if ((ex.ToString().Contains("唯一")))
            {
                LB_UserReg.Text = "会员注册名已经存在,请重新注册!";
                return;
            }
            LB_UserReg.Text = ex.Message;
        }
    }

    private bool CheckData()
    {
        bool rtn = true;
        SqlParameter[] paras ={ 
                new SqlParameter("@userid",SqlDbType.VarChar,30)
            };
        paras[0].Value = TB_ParentID.Text;

        num = (int)SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "UP_RegUserInfo", paras);
        if (num == 2)
        {
            Page.RegisterClientScriptBlock("", "<script language='javascript'> alert('余额不足，不能注册用户！');</script>");
            rtn = false;
        }


        sql = "select count(*) from users where Userid='" + TB_ParentID.Text + "'";
        num = Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text, sql, null));
        if (num == 0)
        {
            Page.RegisterClientScriptBlock("", "<script language='javascript'> alert('查询不到推荐人的帐号，请确认信息是否正确！');</script>");
            rtn = false;
        }
        sql = "select count(*) from users where Userid='" + TB_PaiXian.Text + "'";
        num = Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text, sql, null));
        if (num == 0)
        {
            Page.RegisterClientScriptBlock("", "<script language='javascript'> alert('查询不到安置位置的上级帐号，请确认信息是否正确！');</script>");
            rtn = false;
        }
        sql = "select count(*) from users where Cdefine1='" + TB_PaiXian.Text + "'";
        num = Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text, sql, null));
        if (num >=2)
        {
            Page.RegisterClientScriptBlock("", "<script language='javascript'> alert('安置位置已满，请重新选择安置位置！');</script>");
            rtn = false;
        }
        return rtn;
    }
    #endregion
}
